﻿using Common.Device.DeviceDb;
using Common.Device.Models;
using Common.Device.ViewModels;
using Common.Tool.FileTools;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Common.Device.Views
{
    /// <summary>
    /// TestStationView.xaml 的交互逻辑
    /// </summary>
    public partial class TestStationView : UserControl
    {
        public List<TestListViewModel> channelModels = new List<TestListViewModel>();
        public TestStationView()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 加载测试表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Import_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
                ofd.RestoreDirectory = true;
                ofd.Title = "选择测试文件";
                ofd.Filter = "ZIP文件|*.zip;";
                if (ofd.ShowDialog() == false)
                {
                    return;
                }
                //选择路径
                string selectFile = ofd.FileName;
                //目录下文件名
                var files = new ZipHelper().Decompress(selectFile);
                int no = 0;
                int rows = 0;
                var channelViews = new List<TestChannelView>();
                channelModels.Clear();
                foreach (var item in files)
                {
                    //文件名
                    string fileName = item.Key;
                    //测试表格
                    if (fileName.Contains(".xlsx"))
                    {
                        rows++;
                        no++;
                        var index = no;
                        //读取表格
                        IWorkbook workbook = NpoiHelper.GetWorkbook(item.Key, item.Value);
                        //通道数=sheet
                        int channels = workbook.NumberOfSheets;
                        //遍历通道
                        for (int i = 0; i < channels; i++)
                        {
                            ISheet sheet = workbook.GetSheetAt(i);
                            ObservableCollection<TestOrder> orders = new ObservableCollection<TestOrder>();
                            //遍历表格
                            for (int row = 1; row < sheet.LastRowNum + 1; row++)
                            {
                                //生成测试项
                                TestOrder order = new TestOrder();
                                order.IsCheck = true;
                                order.index = Convert.ToInt32(NpoiHelper.ReadCell(sheet, row, 0));
                                order.name = NpoiHelper.ReadCell(sheet, row, 1);
                                var deviceNames = NpoiHelper.ReadCell(sheet, row, 2).Split(new char[] { '\r', '\n' });
                                var orderTemps = NpoiHelper.ReadCell(sheet, row, 3).Split(new char[] { '\r', '\n' });
                                var orderParameters = NpoiHelper.ReadCell(sheet, row, 4).Split(new char[] { '\r', '\n' });
                                var orderMeses = NpoiHelper.ReadCell(sheet, row, 5).Split(new char[] { '\r', '\n' });
                                var orderRanges = NpoiHelper.ReadCell(sheet, row, 8).Split(new char[] { '\r', '\n' });
                                for (int j = 0; j < deviceNames.Length; j++)
                                {
                                    if (deviceNames[j] != "null" && DeviceService.useDevices.ContainsKey(deviceNames[j]) == false)
                                    {
                                        Common.View.Controls.Dialogs.TipView.Show("未识别到设备" + deviceNames[j]);
                                    }
                                    order.orders.Add(new DeviceOrder()
                                    {
                                        Device = (deviceNames[j] == "null" || !DeviceService.useDevices.ContainsKey(deviceNames[j])) ? null : DeviceService.useDevices[deviceNames[j]],
                                        index = j + 1,
                                        no = Encoding.ASCII.GetString(new[] { (byte)(Encoding.ASCII.GetBytes("a")[0] + j) }),
                                        mes = orderMeses.Length > j ? orderMeses[j] : "",
                                        Order = new Order() { name = deviceNames[j], temp = orderTemps[j], deviceModel = orderParameters[j], range = orderRanges[j] }
                                    });
                                }
                                order.testTimes = Convert.ToInt32(NpoiHelper.ReadCell(sheet, row, 6));
                                order.ngStop = NpoiHelper.ReadCell(sheet, row, 7) == "true";
                                orders.Add(order);
                            }
                            //生成测试通道
                            var view = new Views.TestChannelView();
                            view.viewModel.num = index.ToString().PadLeft(3, '0') + "-" + (i + 1).ToString().PadLeft(3, '0');
                            view.viewModel.Orders = orders;
                            channelModels.Add(view.viewModel);
                            channelViews.Add(view);
                        }
                    }
                }
                StationPannel.Children.Clear();
                foreach (var item in channelViews)
                {
                    //添加测试通道显示
                    StationPannel.Children.Add(item);
                }
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error("加载失败:" + ex.Message);
            }

        }
    }
}
